package com.psg.controller;

import com.psg.pojo.User;
import com.psg.service.OrderService;
import com.psg.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user/order")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @RequestMapping("/list")
    public ModelAndView order(ModelAndView mav,HttpServletRequest request){
        User loginUser = (User)request.getSession().getAttribute("login");
        Integer uid = loginUser.getuId();
        List<com.psg.pojo.Order> orders = orderService.selectOrder(uid);
        request.getSession().setAttribute("order",orders);
        mav.addObject("order",orders);
        mav.setViewName("order");
        return mav;
    }

    @PostMapping("/addOrder")
    @ResponseBody
    public Result addOrder(@RequestParam("ids[]") int[] ids){
        if (ids.length==0) return Result.error("请选择要结算的商品");
        if (null==getUserId()) return Result.error("用户信息不存在！");
        try {
            orderService.generateOrderByCarIds(ids,getUserId());
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("支付失败！");
        }

    }
}
